package com.banciyuan.bcywebview.biz.detail.lock;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.api.CoserApi;
import com.banciyuan.bcywebview.api.DailyApi;
import com.banciyuan.bcywebview.api.IllustApi;
import com.banciyuan.bcywebview.api.NovelApi;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.Complex;
import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by squall on 2016/3/21.
 */
public class LockedHelper {

    private Context mContext;
    private IlockedInter ilockedInter;
    private RequestQueue mQueue;

    public LockedHelper(Context mContext, IlockedInter ilockedInter) {
        this.mContext = mContext;
        this.ilockedInter = ilockedInter;
        this.mQueue = VolleyQueue.getRquest(mContext);
    }

    public void postUpdate(Timeline timeline) {
        String url = null;
        switch (timeline.getTimelineType()) {
            case TimelineFactory.TIMELINE_TYPE_DAILY:
                url = HttpUtils.BASE_URL + DailyApi.getSubmitUnLock();
                break;
            case TimelineFactory.TIMELINE_TYPE_COSER:
                url = HttpUtils.BASE_URL + CoserApi.getSubmitUnLock();
                break;
            case TimelineFactory.TIMELINE_TYPE_ILLUST:
                url = HttpUtils.BASE_URL + IllustApi.getSubmitUnLock();
                break;
            case TimelineFactory.TIMELINE_TYPE_WRITER:
                url = HttpUtils.BASE_URL + NovelApi.getSubmitUnLock();
                break;
        }
        List<BcyPair> pairs = new ArrayList<BcyPair>();
        pairs.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        pairs.add(new BcyPair(HttpUtils.RPID, timeline.getRp_id()));
        if (timeline.getUd_id()!=null&&timeline.getTimelineType()==TimelineFactory.TIMELINE_TYPE_DAILY) {
            pairs.add(new BcyPair(HttpUtils.UDID,timeline.getUd_id()));
        }
        Map<String, String> map = HttpUtils.getData(pairs);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                ilockedInter.succ(response);
            }
        };
        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ilockedInter.fail();
            }
        };

        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        mQueue.add(stringRequestParam);
    }

    public void postUpdate(Complex complex) {
        Timeline timeline = new Timeline();
        timeline.setTimelineType(complex.getTimelineType());
        timeline.setRp_id(complex.getRp_id());
        if (complex.getUd_id()!=null&&complex.getTimelineType()==TimelineFactory.TIMELINE_TYPE_DAILY) {
            timeline.setUd_id(complex.getUd_id());
        }
        postUpdate(timeline);
    }
}
